﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using OfficeOpenXml;
using System.Net;




namespace AdvancedStandingAndScholarship.Web
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Request.Cookies["Authenticated"] == null)
            //    Response.Redirect("/Default.aspx#/MainPage");



        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            if (Server.HtmlEncode(Request.Cookies["Authenticated"].Value) == "y")
            {
                string filename = Server.MapPath("/myfolder/") + FileUpload1.PostedFile.FileName;
                
                //check if any file selected 
                try
                {
                    FileUpload1.PostedFile.SaveAs(filename);
                }
                catch (Exception)
                {
                   // this.RegisterClientScriptBlock(typeof(string), "key",  string.Format("alert('{0}');", ex.Message), true);
                    //Response.Write("<script LANGUAGE='JavaScript'>alert('Exception: ')</script>");

                    /*
                    Response.Write("<script LANGUAGE='JavaScript' >alert('No file selected');document.location='"
                        + ResolveClientUrl("/Default.aspx#/AddSuccessPage") + "';</script>");
                     * */

                    /*
                    HttpCookie notificationCookie = new HttpCookie("notification");
                    notificationCookie.Value = "y";
                    notificationCookie.Expires = DateTime.Now.AddMinutes(1);
                    Response.Cookies.Add(notificationCookie);
                    HttpCookie notificationStatusCookie = new HttpCookie("notificationStatus");
                    notificationStatusCookie.Value = "No File Selected.";
                    notificationStatusCookie.Expires = DateTime.Now.AddMinutes(1);
                    Response.Cookies.Add(notificationStatusCookie);


                    //Response.Redirect("/Default.aspx#/MainPage");

                    Response.Redirect("/Default.aspx#/Browse");
                    */
                }
                FileInfo fi = new FileInfo(filename);

                var existingFile = fi;

                // Open and read the XlSX file.

                using (var package = new ExcelPackage(existingFile))
                {

                    // Get the work book in the file

                    ExcelWorkbook workBook = package.Workbook;

                    if (workBook != null)
                    {
                        if (workBook.Worksheets.Count > 0)
                        {
                            List<Student> studentList = new List<Student>();
                            for (int i = 1; i <= workBook.Worksheets.Count; i++)
                            {
                                ExcelWorksheet currentWorksheet = workBook.Worksheets[i];
                                if (currentWorksheet.Cells[1, 12].Value.ToString() == "CLASS OF HONOURS")
                                {
                                    //graduated

                                    int count = 2;
                                    bool end = false;
                                    while (end == false)
                                    {
                                        try
                                        {
                                            string asd = currentWorksheet.Cells[count, 1].Value.ToString();

                                        }
                                        catch (Exception)
                                        {
                                            end = true;
                                            break;
                                        }
                                        Student temp = new Student();
                                        temp.Student_type = "graduated";

                                        temp.Name = currentWorksheet.Cells[count, 1].Value.ToString();
                                        temp.NRIC1 = currentWorksheet.Cells[count, 2].Value.ToString();
                                        temp.Passport_no = currentWorksheet.Cells[count, 3].Value.ToString();
                                        temp.Gender = currentWorksheet.Cells[count, 4].Value.ToString();
                                        try
                                        {
                                            temp.Dob = Convert.ToDateTime(currentWorksheet.Cells[count, 5].Value.ToString());
                                        }
                                        catch (Exception)
                                        {
                                            double dobNumber = Convert.ToDouble(currentWorksheet.Cells[count, 5].Value.ToString());
                                            temp.Dob = DateTime.FromOADate(dobNumber);
                                        }
                                        temp.Diploma = currentWorksheet.Cells[count, 6].Value.ToString();
                                        temp.Diploma_graduated_year = currentWorksheet.Cells[count, 7].Value.ToString();
                                        temp.Matric_year = currentWorksheet.Cells[count, 8].Value.ToString();
                                        temp.Uni_faculty = currentWorksheet.Cells[count, 9].Value.ToString();
                                        temp.Uni_degree = currentWorksheet.Cells[count, 10].Value.ToString();
                                        temp.Uni_graduated_year = currentWorksheet.Cells[count, 11].Value.ToString();
                                        temp.Uni_name = Server.HtmlEncode(Request.Cookies["selectedSchool"].Value);
                                        temp.Class_of_honours = currentWorksheet.Cells[count, 12].Value.ToString();
                                        temp.Poly_school = currentWorksheet.Name;
                                        try
                                        {
                                            temp.Scholarship = currentWorksheet.Cells[count, 13].Value.ToString();
                                        }
                                        catch (Exception)
                                        {
                                            temp.Scholarship = "";
                                        }
                                        try
                                        {
                                            temp.Awards_dean_list = currentWorksheet.Cells[count, 14].Value.ToString();
                                        }
                                        catch (Exception)
                                        {
                                            temp.Awards_dean_list = "";
                                        }
                                        studentList.Add(temp);
                                        count++;

                                    }
                                }
                                else
                                {
                                    int count = 2;
                                    bool end = false;
                                    while (end == false)
                                    {
                                        try
                                        {
                                            string asd = currentWorksheet.Cells[count, 1].Value.ToString();

                                        }
                                        catch (Exception)
                                        {
                                            end = true;
                                            break;
                                        }
                                        //matriculated
                                        Student temp = new Student();
                                        temp.Student_type = "matriculated";
                                        temp.Name = currentWorksheet.Cells[count, 1].Value.ToString();
                                        temp.NRIC1 = currentWorksheet.Cells[count, 2].Value.ToString();
                                        temp.Passport_no = currentWorksheet.Cells[count, 3].Value.ToString();
                                        temp.Gender = currentWorksheet.Cells[count, 4].Value.ToString();
                                        try
                                        {
                                            temp.Dob = Convert.ToDateTime(currentWorksheet.Cells[count, 5].Value.ToString());
                                        }
                                        catch (Exception)
                                        {
                                            double dobNumber = Convert.ToDouble(currentWorksheet.Cells[count, 5].Value.ToString());
                                            temp.Dob = DateTime.FromOADate(dobNumber);
                                        }
                                        temp.Diploma = currentWorksheet.Cells[count, 6].Value.ToString();
                                        temp.Diploma_graduated_year = currentWorksheet.Cells[count, 7].Value.ToString();
                                        temp.Uni_name = Server.HtmlEncode(Request.Cookies["selectedSchool"].Value);
                                        temp.Uni_application_year = currentWorksheet.Cells[count, 8].Value.ToString();
                                        temp.Uni_faculty = currentWorksheet.Cells[count, 9].Value.ToString();
                                        temp.Uni_degree = currentWorksheet.Cells[count, 10].Value.ToString();
                                        temp.Poly_school = currentWorksheet.Name;

                                        try
                                        {
                                            temp.Ddp_cdp_course = currentWorksheet.Cells[count, 11].Value.ToString();
                                        }
                                        catch (Exception)
                                        {
                                            temp.Ddp_cdp_course = "";
                                        }
                                        try
                                        {
                                            temp.Scholarship = currentWorksheet.Cells[count, 12].Value.ToString();
                                        }
                                        catch (Exception)
                                        {
                                            temp.Scholarship = "";
                                        }
                                        studentList.Add(temp);
                                        count++;

                                    }

                                }
                            }
                            // Get the first worksheet
                            //ExcelWorksheet currentWorksheet = workBook.Worksheets.First();

                            ASASService client = new ASASService();
                            List<Student> newStudentList = client.addNewStudent(studentList, "szesze@live.com");

                            // read some data
                            //object col1Header = currentWorksheet.Cells[1, 1].Value;
                            HttpCookie fileIDCookie = new HttpCookie("fileID123");
                            fileIDCookie.Value = newStudentList[0].File_id.ToString();
                            fileIDCookie.Expires = DateTime.Now.AddMinutes(15);
                            Response.Cookies.Add(fileIDCookie);
                            Response.AppendCookie(fileIDCookie);
                            Response.SetCookie(fileIDCookie);
                        }

                        HttpCookie notificationCookie = new HttpCookie("notification");
                        notificationCookie.Value = "y";
                        notificationCookie.Expires = DateTime.Now.AddMinutes(1);
                        Response.Cookies.Add(notificationCookie);
                        HttpCookie notificationStatusCookie = new HttpCookie("notificationStatus");
                        notificationStatusCookie.Value = "Recorded added successfully.";
                        notificationStatusCookie.Expires = DateTime.Now.AddMinutes(1);
                        Response.Cookies.Add(notificationStatusCookie);


                        //Response.Redirect("/Default.aspx#/MainPage");

                        Response.Redirect("/Default.aspx#/AddSuccessPage");

                        if (Request.Cookies["Authenticated"] != null)
                        {
                            HttpCookie myCookie = new HttpCookie("Authenticated");
                            myCookie.Expires = DateTime.Now.AddDays(-1d);
                            Response.Cookies.Add(myCookie);

                            HttpCookie selectedSchoolCookie = new HttpCookie("selectedSchool");
                            selectedSchoolCookie.Expires = DateTime.Now.AddDays(-1d);
                            Response.Cookies.Add(selectedSchoolCookie);
                        }



                    }
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/Default.aspx#/AddPageSelectSchool");
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/Default.aspx#/AddPage");
        }

    }
}